package com.vv.vv_commonlibrary.page;


import com.vv.vv_commonlibrary.listener.HandlerListDataListener;

import java.util.List;

/**
 * 处理分页状态的逻辑
 *
 * @author xia
 */
public class HandlerPage {


    private int pageNo = 1;
    private int pageSize = 30;

    private HandlerPageListener handlerPageListener;
    private HandlerListDataListener handlerListDataListener;

    public HandlerPage(HandlerPageListener handlerPageListener, HandlerListDataListener handlerListDataListener) {
        this.handlerPageListener = handlerPageListener;
        this.handlerListDataListener = handlerListDataListener;
    }

    public int getPageNo() {
        boolean isRefresh = handlerPageListener.isRefresh();
        if (isRefresh) {
            return 1;
        } else {
            return pageNo;
        }
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageSize() {
        return pageSize;
    }

    public <T> void handlerPageBySize(List<T> listData) {
        handlerPageBySize(listData, pageSize);
    }

    public <T> void handlerPageBySize(List<T> listData, int pageSize) {
        boolean isRefresh = handlerPageListener.isRefresh();
        if (listData != null) {
            int size = listData.size();
            if (isRefresh) {
                handlerListDataListener.freshDataToList(listData);
                handlerPageListener.finishRefresh();
                if (size == 0) {
                    handlerPageListener.showEmptyView();
                } else {
                    if (size < pageSize) {
                        handlerPageListener.handlerNoMoreData();
                    } else {
                        handlerPageListener.refreshHaveMoreData();
                        pageNo = 2;
                    }
                }
            } else {
                handlerListDataListener.addDataToList(listData);
                if (size < pageSize) {
                    handlerPageListener.handlerNoMoreData();
                } else {
                    handlerPageListener.refreshHaveMoreData();
                    pageNo++;
                }
            }
        } else {
            if (isRefresh) {
                handlerPageListener.finishRefresh();
            }
        }
    }

}
